package elasticsearch;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.action.admin.indices.get.GetIndexResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.alibaba.fastjson.JSON;
import com.es.search.Application;

@SuppressWarnings("deprecation")
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
public class SearchJunit {
	@Autowired
    private RestHighLevelClient client;
	@Test
    // 文档查询
   public void testSearchDocument() throws Exception{
	     // 1.创建查询请求
        SearchRequest searchRequest = new SearchRequest("hhl_index");

        // 2.创建SearchSourceBuilder
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        // 3.创建查询条件，matchAllQuery 查询所有
        MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
        //TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "杜兰特");

        // 4.将查询条件放入searchSourceBuilder
        searchSourceBuilder.query(matchAllQueryBuilder);
        searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));

        // 5.将searchSourceBuilder放入查询请求中
        searchRequest.source(searchSourceBuilder);

        // 6.发送查询请求
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

        // 7.遍历查询结果 hits()
        System.out.println(JSON.toJSONString(searchResponse.getHits()));
        System.out.println("==============SearchHit===============");
        for (SearchHit documentFields : searchResponse.getHits().getHits()) {
            System.out.println(documentFields.getSourceAsMap());
        }
    }
	
	/**
     * 通过ID，查询指定文档
     * @throws IOException
     */
    //@Test
    public void getById() throws IOException {
        // 创建索引,即获取索引
        GetRequest request = new GetRequest();
        // 外层参数
        request.id("1");
        request.index("cs_index");
        request.type("_doc");
        // 发送请求
        System.out.println(request.toString());
        GetResponse response = client.get(request, RequestOptions.DEFAULT);
        System.out.println(response.toString());
    }
    
    
    
    /**
     * 查询索引
     * @throws IOException
     */
    //@Test
    public void getIndex() throws IOException {
        // 创建请求
        GetIndexRequest request = new GetIndexRequest();
        request.indices("cs_index");
        // 执行请求,获取响应
        GetIndexResponse response = client.indices().get(request, RequestOptions.DEFAULT);
        System.out.println(response.toString());
    }
    
    /**
     * 检查索引是否存在
     * @throws IOException
     */
    //@Test
    public void exists() throws IOException {
        // 创建请求
        GetIndexRequest request = new GetIndexRequest();
        request.indices("cs_index");
        // 执行请求,获取响应
        boolean response = client.indices().exists(request, RequestOptions.DEFAULT);
        System.out.println(response);
    }
    
    /**
     * 查询所有的索引名称
     * @throws IOException
     */
    //@Test
    public void getAllIndices() throws IOException {
//        GetAliasesRequest request = new GetAliasesRequest();
//        GetAliasesResponse response =  client.indices().getAlias(request,RequestOptions.DEFAULT);
//        Map<String, Set<AliasMetaData>> map = response.getAliases();
//        Set<String> indices = map.keySet();
//        for (String key : indices) {
//            System.out.println(key);
//        }
    }
    
}
